package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayVillage;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.service.mongo.web.SundayVillageService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/8/1
 * @Description：小区
 */
@Controller
@RequestMapping("/sunday/web/village")
public class SundayVillageController extends BaseController{
    @Autowired
    private SundayVillageService villageService;

    @RequestMapping("/index" )
    public  String index(HttpServletRequest request){
        return "/sunday/village/index";
    }

    @RequestMapping("/input" )
    public  String input(HttpServletRequest request
            ,@RequestParam("id") Long id
            ,Model model){
        model.addAttribute("village",villageService.findOne(id));
        return "/sunday/village/input";
    }
    /**
     * 新增或修改仓库信息
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="id") Long id
            ,@RequestParam(value="name") String name
            ,@RequestParam(value="type") Integer type
            ,@RequestParam(value="desc",required=false) String desc
            ,@RequestParam(value="provinceId") Long provinceId
            ,@RequestParam(value="provinceName") String provinceName
            ,@RequestParam(value="cityId") Long cityId
            ,@RequestParam(value="cityName") String cityName
            ,@RequestParam(value="districtId") Long districtId
            ,@RequestParam(value="districtName") String districtName
            ,@RequestParam(value="address") String address
            ,@RequestParam(value="longitude") Double longitude
            ,@RequestParam(value="latitude") Double latitude
            ,@RequestParam(value="imageFile",required=false) MultipartFile imageFile
            ,@RequestParam(value="areaId") Long areaId
    ) {
        try {
            SundayVillage village=new SundayVillage( name,  null,  type, provinceId,  provinceName,
                    cityId,  cityName,  districtId,  districtName,  address,  longitude,  latitude, desc, areaId);
            villageService.save(id,village,imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/village/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request
            , HttpServletResponse response){
        try {
            PageResult<SundayVillage> result = villageService.selectAll(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }


    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       @RequestParam(value="id",required=true) Long id){
        try {
            villageService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/village/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }


}
